function tree(data, option) {
    let obj = {},
        defaults = { topTitle: '顶级分类' },
        opts = Object.assign(defaults, option);
    
    if (data.length === 0) {
        return {
            id: 0,
            title: '顶级分类',
            children: [],
            path: '-1'
        };
    }

    for (let i = 0; i < data.length; i++) {
        if (!obj[data[i].parent_id]) {
            obj[data[i].parent_id] = {};
            obj[data[i].parent_id].children = [];
        }
        obj[data[i].parent_id].children.push(data[i]);
    }
    obj[0].title = opts.topTitle;
    obj[0].id = 0;
    obj[0].path = '-1';
    for (const k in obj) {
        if (obj.hasOwnProperty(k)) {
            let children = obj[k].children;
            if (!children) return;
            for (let i = 0; i < children.length; i++) {
                if (obj.hasOwnProperty(children[i].id)) {
                    children[i].children = obj[children[i].id].children;
                }
            }
        }
    }

    return obj[0];
}

export default tree;
